package com.fyyg.wechat.util.wechat;

import java.util.Date;
import java.util.Map;
import java.util.Set;

import com.fyyg.common.redis.RedisBase;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.Tuple;

public class RedisUtils {
	
    
    public static void main(String[] args)
    {
//        redis.set("test", "abc");
        String s = redis.get("test");
        System.out.println(s);
    }

	protected static ShardedJedis redis = RedisBase.getInstance().getResource();

	protected static void set(String entityName, String id, String fieldName,
			String value) {
		String key = createKey(entityName, id, fieldName);
		redis.set(key, value);
	}
	
	protected static void hmSet(String entityName,String id,String fieldName,Map<String, String>map){
		String key = createKey(entityName, id, fieldName);
		redis.hmset(key, map);
	}
	
	protected static Map<String, String> hmGet(String entityName,String id,String fieldName) {
		String key = createKey(entityName, id, fieldName);
		Map<String, String> map = redis.hgetAll(key);
		return map;
	}
	
	protected static long ttl(String entityName, String id, String fieldName){
		String key = createKey(entityName, id, fieldName);
		return redis.ttl(key);
	}

	protected static String setNx(String entityName, String id,
			String fieldName, String value, String nxxx, String expx, long time) {
		String key = createKey(entityName, id, fieldName);
		String rtn = redis.set(key, value, nxxx, expx, time);
		return rtn;
	}

	protected static Long del(String entityName, String id, String fieldName) {
		String key = createKey(entityName, id, fieldName);
		return redis.del(key);
	}

	private static String createKey(String entityKey, String id,
			String fieldName) {
		StringBuffer key = new StringBuffer();
		key.append(entityKey);
		key.append(":");
		key.append(id);
		key.append(":");
		key.append(fieldName);
		return key.toString();
	}

	protected static String get(String entityName, String id, String fieldName) {
		String key = createKey(entityName, id, fieldName);
		String stock = redis.get(key);
		return stock;
	}

	protected static long incr(String entityName, String id, String fieldName) {
		String key = createKey(entityName, id, fieldName);
		return redis.incr(key).longValue();
	}

	protected static long expire(String entityName, String id,
			String fieldName, Date date) {
		String key = createKey(entityName, id, fieldName);
		Date current = new Date();
		return redis.expire(key, Long.valueOf(((date.getTime() - current.getTime())/1000)).intValue());
	}
	

	protected static long decr(String entityName, String id, String fieldName) {
		String key = createKey(entityName, id, fieldName);
		return redis.decr(key).longValue();
	}

	protected static void zadd(String entityName, String scoreGroupName,
			double score, String member) {
		String sortedSetKey = createSortedSetKey(entityName, scoreGroupName);

		redis.zadd(sortedSetKey, score, member);
	}

	protected static Set<Tuple> zrevrange(String entityName,
			String scoreGroupName, int offset, int count) {
		String sortedSetKey = createSortedSetKey(entityName, scoreGroupName);

		Set<Tuple> tupleSet = redis.zrevrangeByScoreWithScores(sortedSetKey,
				"+inf", "-inf", offset, count);

		return tupleSet;
	}

	protected static Long zrank(String entityName, String scoreGroupName,
			String member) {
		String sortedSetKey = createSortedSetKey(entityName, scoreGroupName);

		Long rank = redis.zrank(sortedSetKey, member);

		return rank;
	}

	protected static Long zrevrank(String entityName, String scoreGroupName,
			String member) {
		String sortedSetKey = createSortedSetKey(entityName, scoreGroupName);

		Long rank = redis.zrevrank(sortedSetKey, member);

		return rank;
	}
	
	protected static void rpush(String entityName, String id, String fieldName, String value){
	    String key = createKey(entityName, id, fieldName);
	    redis.rpush(key, value);
	}

	protected static Double zscore(String entityName, String scoreGroupName,
			String member) {
		String sortedSetKey = createSortedSetKey(entityName, scoreGroupName);

		Double score = redis.zscore(sortedSetKey, member);

		return score;
	}

	private static String createSortedSetKey(String entityName,
			String scoreGroupName) {
		StringBuffer key = new StringBuffer();
		key.append(entityName);
		key.append(":");
		key.append(scoreGroupName);
		String sortedSetKey = key.toString();
		return sortedSetKey;
	}
}
